<template>
  <div class="app-container">
    <div class="content-box">
      <el-collapse v-model="activeName" accordion>
        <el-collapse-item title="操作提示" name="1">
          <ul>
            <li>
              当前显示的是商家发布过的所有商品，当商品处于上架状态时前台显示
            </li>
            <li>虚拟商品用户购买之后可以通过订单核销进行商品核销处理。</li>
            <li>
              如果商家的商品操作违规，平台可以操作违规下架，违规下架的商品需要商家编辑审核之后才能重新上架
            </li>
          </ul>
        </el-collapse-item>
      </el-collapse>
      <div class="moban_box">
        导入模板：<a :href="downloadUrl" style="color: #0766fe">商品规格模板</a>
      </div>
      <el-form
        v-show="showSearch"
        ref="queryForm"
        :model="queryParams"
        :inline="true"
        class="sreach-form-content"
        label-width="100px"
      >
        <!-- <el-form-item label="商品id" prop="goods_id">
          <el-input
            v-model="queryParams.goods_id"
            placeholder="请输入"
            clearable
            size="small"
          />
        </el-form-item> -->
        <!-- <el-form-item label="商品编码" prop="goods_sn">
          <el-input
            v-model="queryParams.goods_sn"
            placeholder="请输入"
            clearable
            size="small"
          />
        </el-form-item> -->
        <!-- <el-form-item label="所在仓库" prop="warehouse">
          <el-select
            v-model="queryParams.warehouse"
            placeholder="请选择"
            clearable
            size="small"
          >
            <el-option
              v-for="(item, index) in mapp_list.warehouse"
              :key="index"
              :label="item"
              :value="index"
            />
          </el-select>
        </el-form-item> -->
        <el-form-item label="商品分类" prop="cate_id">
          <el-cascader
            :options="mapp_list.cate"
            :show-all-levels="false"
            placeholder="请选择"
            :props="props"
            v-model="queryParams.cate_id"
            filterable
            clearable
            size="small"
            @change="sassa"
          ></el-cascader>
        </el-form-item>
        <el-form-item label="品牌名称" prop="brand_id">
          <el-select
            v-model="queryParams.brand_id"
            placeholder="请选择"
            clearable
            filterable
            size="small"
          >
            <el-option
              v-for="(item, index) in mapp_list.brand"
              :key="index"
              :label="item"
              :value="index"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="商品标题" prop="title">
          <el-input
            v-model="queryParams.title"
            placeholder="请输入"
            clearable
            size="small"
          />
        </el-form-item>
        <!-- <el-form-item label="副标题" prop="subtitle">
          <el-input
            v-model="queryParams.subtitle"
            placeholder="请输入"
            clearable
            size="small"
            @keyup.enter.native="handleQuery"
          />
        </el-form-item> -->
        <!-- <el-form-item label="关键词" prop="keywords">
          <el-input
            v-model="queryParams.keywords"
            placeholder="请输入"
            clearable
            size="small"
          />
        </el-form-item> -->
        <el-form-item label="规格类型" prop="spec_type">
          <el-select
            v-model="queryParams.spec_type"
            placeholder="请选择"
            clearable
            size="small"
          >
            <el-option label="所有" value="" />
            <el-option label="单规格" value="1" />
            <el-option label="多规格" value="2" />
          </el-select>
        </el-form-item>
        <!-- <el-form-item label="推荐到欧洲" prop="is_oz">
            <el-select
              v-model="queryParams.is_oz"
              placeholder="请选择"
              clearable
              size="small"
            >
              <el-option label="所有" value="" />
              <el-option label="是" value="1" />
              <el-option label="否" value="2" />
            </el-select>
          </el-form-item> -->
        <el-form-item label="上架" prop="status">
          <el-select
            v-model="queryParams.status"
            placeholder="请选择"
            clearable
            size="small"
          >
            <el-option label="所有" value="" />
            <el-option label="是" value="1" />
            <el-option label="否" value="2" />
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button
            type="primary"
            icon="el-icon-search"
            size="mini"
            @click="handleQuery"
            >搜索</el-button
          >
          <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
            >重置</el-button
          >
        </el-form-item>
      </el-form>
      <el-row :gutter="10" class="mb8">
        <el-col :span="1.5">
          <el-button
            plain
            type="primary"
            icon="el-icon-plus"
            size="mini"
            @click="handleAdd"
            >添加</el-button
          >
          <el-button
            plain
            type="danger"
            size="mini"
            icon="el-icon-delete-solid"
            @click="delAllBrand"
            >删除</el-button
          >
          <el-upload
            class="upload-demo"
            :action="upload.url"
            :on-success="goodsImports"
            :show-file-list="false"
            multiple
          >
            <el-button plain type="warning" icon="el-icon-bottom" size="mini"
              >导入商品</el-button
            >
          </el-upload>
          <el-upload
            class="upload-demo"
            :action="upload.url"
            :on-success="skuImports"
            :show-file-list="false"
            multiple
          >
            <el-button plain type="warning" icon="el-icon-bottom" size="mini"
              >导入规格</el-button
            >
          </el-upload>

          <el-button
            plain
            type="warning"
            size="mini"
            icon="el-icon-top"
            @click="Commodityexport"
            >导出商品</el-button
          >
          <el-dropdown trigger="click" @command="handleCommand">
            <el-button
              plain
              type="info"
              size="mini"
              :disabled="select_list.length > 0 ? false : true"
            >
              批量操作<i class="el-icon-arrow-down el-icon--right" />
            </el-button>
            <template #dropdown>
              <el-dropdown-menu>
                <el-dropdown-item icon="el-icon-top" :command="1"
                  >设置上架</el-dropdown-item
                >
                <el-dropdown-item icon="el-icon-bottom" :command="2"
                  >设置下架</el-dropdown-item
                >
              </el-dropdown-menu>
            </template>
          </el-dropdown>
        </el-col>
        <right-toolbar :show-search.sync="showSearch" @queryTable="getList" />
      </el-row>
      <el-table
        v-loading="loading"
        :data="menuList"
        row-key="goods_id"
        :tree-props="{ children: 'children', hasChildren: 'hasChildren' }"
        @selection-change="handleSelectionChange"
        @cell-dblclick="to_hasPermi() ? handleEdit($event) : ''"
      >
        <el-table-column type="selection" width="42" align="center" />
        <el-table-column prop="goods_id" label="ID" width="55" align="center" />
        <el-table-column label="商品图片" align="center" width="150">
          <template slot-scope="scope">
            <el-image
              class="good_img"
              :src="scope.row.image"
              :preview-src-list="[scope.row.image]"
            >
            </el-image>
          </template>
        </el-table-column>
        <el-table-column prop="title" label="商品标题" align="center" />
        <el-table-column prop="brand_id" label="品牌名称" align="center">
          <template slot-scope="scope">
            <div>{{ scope.row.brand_id }}</div>
          </template>
        </el-table-column>
        <el-table-column
          prop="supply_price"
          label="供货价"
          align="center"
          width="130"
        />
        <el-table-column
          prop="stock"
          label="库存信息"
          width="150"
          align="center"
        >
          <template slot-scope="scope">
            {{ scope.row.stock > 1 ? scope.row.stock : "需补货" }}
          </template>
        </el-table-column>
        <el-table-column
          label="规格类型"
          align="center"
          prop="spec_type_text"
          width="76"
        >
        </el-table-column>
        <el-table-column label="状态" width="92" align="center">
          <template slot-scope="scope">
            {{ scope.row.status == 1 ? "上架" : "下架" }}
          </template>
        </el-table-column>
        <el-table-column
          label="操作"
          align="center"
          class-name="small-padding fixed-width"
        >
          <template slot-scope="scope">
            <el-button
              size="mini"
              type="text"
              @click="change_recommend(1, scope.row)"
              >上架</el-button
            >
            <el-button
              size="mini"
              type="text"
              @click="change_recommend(2, scope.row)"
              >下架</el-button
            >
            <el-button size="mini" type="text" @click="handleEdit(scope.row)"
              >编辑</el-button
            >
            <el-button size="mini" type="text" @click="handleDelete(scope.row)"
              >删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>
      <pagination
        v-if="totalPage > 0"
        :total="totalPage"
        :page="queryParams.page"
        :limit="queryParams.pageSize"
        @pagination="changepage"
      />
    </div>
  </div>
</template>

<script>
import pagination from "@/components/Pagination";
// import to_hasPermis from "@/utils/to_hasPermis";

import {
  listgoods,
  yinseSearch,
  goodclass,
  goodbrand,
  delgoods,
  multi,
} from "@/api/commodity";
export default {
  name: "goodsManage",
  components: {
    pagination,
  },
  data() {
    return {
      // 下载模板
      downloadUrl: process.env.VUE_APP_BASE_API + `goods_sku.xlsx`,
      // 遮罩层
      loading: true,
      // 显示搜索条件
      showSearch: true,
      // 菜单表格树数据
      menuList: [],
      // 菜单树选项
      menuOptions: [],
      // 弹出层标题
      title: "",
      // 选中的商品
      select_list: [],
      // 是否显示弹出层
      open: false,
      mapp_list: {},
      props: {
        value: "id",
        label: "cate_name",
        children: "next",
      },
      // 查询参数
      queryParams: {
        // goods_id: "",
        // goods_sn: "",
        cate_id: "",
        brand_id: "",
        title: "",
        // subtitle: "",
        // keywords: "",
        spec_type: "",
        // is_oz: "",
        status: "",
        // warehouse: "",
        page: 1,
        pageSize: 10,
      },
      totalPage: 0,
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        menuName: [
          { required: true, message: "菜单名称不能为空", trigger: "blur" },
        ],
        orderNum: [
          { required: true, message: "菜单顺序不能为空", trigger: "blur" },
        ],
        path: [
          { required: true, message: "路由地址不能为空", trigger: "blur" },
        ],
      },
      url: process.env.VUE_APP_BASE_API + "goods/spu/export",
      // 用户导入参数
      upload: {
        // 是否禁用上传
        isUploading: false,
        // 设置上传的请求头部
        headers: {},
        // 上传的地址
        url: process.env.VUE_APP_BASE_API + "common/upload",
      },
      activeName: "1",
    };
  },
  created() {
    this.getList();
    this.mapping_list();
  },
  activated() {
    this.getList();
  },
  methods: {
    sassa(e) {
      if (e.length > 0) {
        this.queryParams.cate_id = e[e.length - 1];
      } else {
        this.queryParams.cate_id = "";
      }
    },
    to_hasPermi() {
      return to_hasPermis("goods:spu:edit");
    },
    // 批量操作
    handleCommand(e) {
      const ids = [];
      this.select_list.forEach((item) => {
        ids.push(item);
      });
      const info = {
        goods_id: ids.join(","),
        field_key: "status",
        field_val: e,
      };
      multi(info).then((res) => {
        this.getList();
      });
    },
    // 选择表格
    handleSelectionChange(e) {
      console.log(e);
      this.select_list = [];
      e.forEach((item) => {
        this.select_list.push(item.goods_id);
      });
    },
    // 商品导入
    goodsImports(res) {
      goodsImport({ file: res.data.url }).then((res) => {
        this.$message({ message: "导入成功", type: "success" });
        this.getList();
      });
    },
    // sku导入
    skuImports(res) {
      skuImport({ file: res.data.url }).then((res) => {
        this.$message({ message: "导入成功", type: "success" });
        this.getList();
      });
    },
    // 商品导出
    Commodityexport() {
      const data = this.queryParams;
      let str =
        this.url +
        // "?warehouse=" +
        // data.warehouse +
        "&goods_id=" +
        data.goods_id +
        "&goods_sn=" +
        data.goods_sn +
        "&cate_id=" +
        data.cate_id +
        "&brand_id=" +
        data.brand_id +
        "&title=" +
        data.title +
        "&subtitle=" +
        data.subtitle +
        "&keywords=" +
        data.keywords +
        "&spec_type=" +
        data.spec_type +
        // "&is_oz=" +
        // data.is_oz +
        "&status=" +
        data.status;
      if (this.select_list.length > 0) {
        str += "&ids=" + this.select_list.join(",");
      }
      window.open(str, "_blank");
    },

    // 获取搜索需要的内容
    mapping_list() {
      yinseSearch().then((res) => {
        this.goodclass(res.data);
        this.goodbrand();
      });
    },
    // 获取品牌
    goodbrand() {
      goodbrand().then((res) => {
        let data = this.mapp_list;
        data.brand = res.data;
        this.mapp_list = {
          ...data,
        };
      });
    },
    // 获取分类
    goodclass(data) {
      goodclass().then((res) => {
        let datas = data;
        datas.cate = res.data;
        this.mapp_list = {
          ...datas,
        };
        console.log(this.mapp_list);
      });
    },
    // 分页
    changepage(e) {
      this.queryParams.page = e.page;
      this.queryParams.pageSize = e.limit;
      this.getList();
    },
    change_recommend(status, row) {
      console.log(row);
      const info = {
        ids: row.id,
        params: "status-" + status,
      };
      editgoods(info).then((res) => {
        this.getList();
      });
    },
    /** 查询菜单列表 */
    getList() {
      this.loading = false;
      const info = {
        page: this.queryParams.page,
      };

      // if (this.queryParams.goods_id) {
      //   info.goods_id = this.queryParams.goods_id;
      // }
      // if (this.queryParams.warehouse) {
      //   info.warehouse = this.queryParams.warehouse;
      // }
      if (this.queryParams.cate_id) {
        info.cate_id = this.queryParams.cate_id;
      }
      if (this.queryParams.brand_id) {
        info.brand_id = this.queryParams.brand_id;
      }
      // if (this.queryParams.goods_sn) {
      //   info.goods_sn = this.queryParams.goods_sn;
      // }
      if (this.queryParams.title) {
        info.title = this.queryParams.title;
      }
      // if (this.queryParams.subtitle) {
      //   info.subtitle = this.queryParams.subtitle;
      // }
      // if (this.queryParams.is_oz) {
      //   info.is_oz = this.queryParams.is_oz;
      // }
      if (this.queryParams.spec_type) {
        info.spec_type = this.queryParams.spec_type;
      }
      if (this.queryParams.status) {
        info.status = this.queryParams.status;
      }
      // if (this.queryParams.keywords) {
      //   info.keywords = this.queryParams.keywords;
      // }
      listgoods(info).then((res) => {
        this.menuList = res.data.list;
        this.totalPage = res.data.total_page;
        this.loading = false;
      });
    },

    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.page = 1;

      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.$router.push({
        name: "goodsAdd",
        params: {
          type: 1,
        },
      });
    },
    // 跳转编辑带id
    handleEdit(res) {
      console.log(res);
      this.$router.push({
        path: "/commodityManagement/goodsEdit/" + res.goods_id,
      });
    },
    /** 删除选择的商品 */
    delAllBrand() {
      this.$confirm("是否确认删除这些商品?", "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          let id = [];
          this.select_list.forEach((item) => {
            id.push(item);
          });
          id = id.join(",");
          return delgoods({ goods_id: id });
        })
        .then(() => {
          this.getList();
          this.$message("删除成功");
        });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      console.log(row);
      this.$confirm('是否确认删除名称为"' + row.title + '"的数据项?', "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(function () {
          return delgoods({ goods_id: row.goods_id });
        })
        .then(() => {
          this.getList();
          this.$message("删除成功");
        });
    },
  },
};
</script>
<style lang="scss" scoped>
.upload-demo {
  display: inline-block;
}
.app-container {
  background-color: #fafafa;
  width: 100%;
  min-height: 91vh;
}
.moban_box {
  padding: 0 18px 21px;
  background: no-repeat;
}
.search_box {
  .li {
    width: 30%;
    align-items: center;
    margin-bottom: 25px;
    .el-input {
      width: 80%;
    }
  }
}
.good_img {
  width: 100px;
  height: 100px;
}
.pice_box {
  width: 100px;
}
.el-button {
  margin-right: 10px;
  margin-left: 0;
}
.table_info {
  width: 60%;
  text-align: left;
  margin-left: 20px;
}
.left_price {
  text-align: left;
}
</style>
